package week_02.gather.gather_unordered;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;

public class gather_unordered {
    public static void main(String[] args) throws
            IOException {

        String pathName = "src\\week_02\\gather\\1w random number.txt";
        FileReader fileReader = new FileReader(pathName);
        BufferedReader bufferedReader = new BufferedReader(fileReader);
        String textLine = "";
        String str = "";
        while ((textLine = bufferedReader.readLine()) != null) {
            str += textLine;
        }
        Integer[] aAfter = new Integer[10000];
        Integer[] aForm = new Integer[10000];
        Integer[] bAfter = new Integer[100];
        Integer[] bForm = new Integer[100];
        //收集交集的数组
        ArrayList<Integer> new_arrA = new ArrayList<Integer>();
        ArrayList<Integer> new_arrB = new ArrayList<Integer>();
        //收集并集的数组
        ArrayList<Integer> new_arrUa = new ArrayList<>();
        ArrayList<Integer> new_arrUb = new ArrayList<>();
        //用泛型收集的数组
        ArrayList<Integer> aAfterT = new ArrayList<Integer>();
        ArrayList<Integer> aFormT = new ArrayList<Integer>();
        ArrayList<Integer> bAfterT = new ArrayList<Integer>();
        ArrayList<Integer> bFormT = new ArrayList<Integer>();
        String[] arr = str.split(",");
        //导入长度为10000的数组
        for (int i = 0; i < aAfter.length; i++) {
            aAfter[i] = Integer.parseInt(arr[i]);
        }
        for (int i = 0; i < aForm.length; i++) {
            aForm[i] = Integer.parseInt(arr[i]);
        }
        //导入长度为100的数组
        for (int i = 0; i < bAfter.length; i++) {
            bAfter[i] = Integer.parseInt(arr[i]);
        }
        for (int i = 0; i < bForm.length; i++) {
            bForm[i] = Integer.parseInt(arr[i]);
        }
        //前后1000个数的数组
        aAfter = Arrays.copyOfRange(aAfter, 9000, 10000);
        aForm = Arrays.copyOfRange(aForm, 0, 1000);

        //前后100个数的数组
        bAfter = Arrays.copyOfRange(aAfter, 900, 1000);
        bForm = Arrays.copyOfRange(aForm, 0, 100);
        //把数组放到泛型里面
        aAfterT.addAll(Arrays.asList(aAfter));
        aFormT.addAll(Arrays.asList(aForm));
        bAfterT.addAll(Arrays.asList(bAfter));
        bFormT.addAll(Arrays.asList(bForm));
//        Collections.sort(aAfterT);
//        Collections.sort(aFormT);
//        Collections.sort(bAfterT);
//        Collections.sort(bFormT);
        long starTime = System.currentTimeMillis();
        for (int i = 0; i < bFormT.size(); i++) {
            if (bFormT.contains(bAfterT.get(i))) {
                new_arrB.add(bAfterT.get(i));
            }
        }
        for (int i = 0; i < aFormT.size(); i++) {
            if (aFormT.contains(aAfterT.get(i))) {
                new_arrA.add(aAfterT.get(i));
            }
        }
        new_arrUa.addAll(aAfterT);
        new_arrUa.addAll(aFormT);
        new_arrUb.addAll(bAfterT);
        new_arrUb.addAll(bFormT);
        for (int i = 0; i < aFormT.size(); i++) {
            if (new_arrUa.contains(aAfterT.get(i))) {
                new_arrUa.remove(aAfterT.get(i));
            }
        }
        for (int i = 0; i < bFormT.size(); i++) {
            if (new_arrUa.contains(bAfterT.get(i))) {
                new_arrUa.remove(bAfterT.get(i));
            }
        }
        long endTime=System.currentTimeMillis();
        long time = endTime-starTime;
        System.out.println(time+"ms");
        System.out.println("前后1000个数的交集:" + new_arrA);
        System.out.println("前后100个数的交集:" + new_arrB);
        System.out.println("前后1000个数的并集:" + new_arrUa);
        System.out.println("前后100个数的并集:" + new_arrUb);
    }
}
